package com.chixing.dao.impl;

import com.chixing.dao.EmployeeDao;
import com.chixing.dao.JdbcTemplate;
import com.chixing.pojo.Employee;

import java.util.List;

public class EmployeeDaoImpl implements EmployeeDao {
    private JdbcTemplate<Employee> jdbcTemplate;

    public EmployeeDaoImpl(JdbcTemplate<Employee> jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public Employee selectByPrimaryKey(int primaryKey) {
        String sql = "select empno,ename,job,hiredate,mgr,salary,comm,deptno from emp where empno = ?";
        Object[] args = {primaryKey};
        return jdbcTemplate.queryForObject(sql,args);
    }

    @Override
    public List<Employee> selectAll() {
        String sql = "select empno,ename,job,hiredate,mgr,salary,comm,deptno from emp ";
        return jdbcTemplate.queryForList(sql,null);
    }

    @Override
    public int insert(Employee emp) {
        String sql = "insert into emp values(?,?,?,?,?,?,?,?)";
        Object[] args =  {emp.getEmpno(),emp.getEmpName(),emp.getEmpJob(),emp.getEmpHiredate(),
                        emp.getEmpMgr(),emp.getEmpSalary(),emp.getEmpComm(),emp.getDeptno()};
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public int update(Employee emp) {
        String sql = "update emp set ename =?,job = ?,mgr = ?,hiredate = ?,salary = ?,comm = ? ,deptno = ?   where empno = ?";
        Object[] args = { emp.getEmpName(),emp.getEmpJob(), emp.getEmpMgr(),emp.getEmpHiredate(),
                          emp.getEmpSalary(),emp.getEmpComm(),emp.getDeptno(),  emp.getEmpno()};
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public int delete(int primaryKey) {
        String sql = "delete from emp where empno = ?";
        Object[] args = {primaryKey};
        return jdbcTemplate.update(sql,args);
    }
}
